﻿using UnityEngine;
using System.Collections;
using System;
using System.Collections.Generic;

namespace UI
{
    public class RecycleResetPanel : MonoBehaviour, ISubContent
    {
        public List<UITweener> tweeners;
        public CommonIcon ResetIcon;
        public GameObject costObj;
        public UILabel costLabel;

        public Animator wheelAnim;
        public GameObject effect;

        public GameObject effectMask;

        public const string resetAudio = "recycle_reset";


        public void OnResetIconClick()
        {
            if (StaticData.IsRecycleResetSelect())
            {
                PageManager.Instance.OpenPage("RecycleResetSelectPage", "type=" + StaticData.curRecycleResetSelectType);
            }
            else {
                PageManager.Instance.OpenPage("RecycleResetSelectPage", "");
            }
        }

        public void OnOpenSubPageTweenOver()
        {

        }

        IEnumerator ConfirmReset()
        {
            var mask = Instantiate(Resources.Load("prefab/TopMask")) as GameObject;
            mask.transform.parent = GameObject.Find("/UI Root").transform;
            mask.gameObject.SetActive(true);
            AudioManager.Instance.PlayUIAudio(resetAudio);
            
            effect.SetActive(true);
            wheelAnim.Play(RecyclePage.wheelAniSpeedupName);
            effectMask.SetActive(true);
            yield return new WaitForSeconds(1.0f);
            Refresh();
            yield return new WaitForSeconds(1.4f);
            effect.SetActive(false);
            wheelAnim.Play(RecyclePage.wheelAniNormalName);
            PageManager.Instance.OpenPage("RecycleRewardPage", "type=1");
            effectMask.SetActive(false);

            Destroy(mask.gameObject);
        }

        public void OnConfirmResetButtonClick()
        {
            if (StaticData.recycleResetSelect.Count == 0)
            {
                BoxManager.CreatePopupTextBox(TableTextBlo.GetText("recycle_select_count_warning"));
                return;
            }

            if (StaticData.playerData.diamond < curCost)
            {
                BoxManager.CreatePopupTextBox(TableTextBlo.GetText("diamond_lack"));
                return;
            }
            if (StaticData.curRecycleResetSelectType == 0)
            {
                ProtoBuf.Message.CSWarriorReset msg = new ProtoBuf.Message.CSWarriorReset();
                msg.guid = StaticData.recycleResetSelect[0];
                NetAPI.SendHttp(ProtoBuf.Message.OpDefine.CSWarriorReset, msg, (pack, b) => {
                    if (b)
                    {
                        ProtoBuf.Message.SCWarriorReset _msg = pack.kBody as ProtoBuf.Message.SCWarriorReset;
                        StaticData.playerData.Delete(_msg.cost);
                        StaticData.playerData.AddResultObj(_msg.get);
                        foreach (var war in _msg.warriors)
                        {
                            StaticData.playerData.UpdateHero(war);
                        }
                        StaticData.recycleReward = _msg.get;

                        StaticData.ClearRecycleResetSelect();
                        StartCoroutine(ConfirmReset());
                    }
                });
            }
            else if (StaticData.curRecycleResetSelectType == 1)
            {
                ProtoBuf.Message.CSEquipReBuild msg = new ProtoBuf.Message.CSEquipReBuild();
                msg.guid = StaticData.recycleResetSelect[0];
                NetAPI.SendHttp(ProtoBuf.Message.OpDefine.CSEquipReBuild, msg, (pack, b) => {
                    if (b)
                    {
                        ProtoBuf.Message.SCEquipReBuild _msg = pack.kBody as ProtoBuf.Message.SCEquipReBuild;
                        StaticData.playerData.Delete(_msg.cost);
                        StaticData.playerData.AddResultObj(_msg.get);
                        StaticData.recycleReward = _msg.get;
                        StaticData.ClearRecycleResetSelect();
                        StartCoroutine(ConfirmReset());
                    }
                });
            }
            else if (StaticData.curRecycleResetSelectType == 2)
            {
                ProtoBuf.Message.CStreasureRebuild msg = new ProtoBuf.Message.CStreasureRebuild();
                msg.targetId = StaticData.recycleResetSelect[0];
                NetAPI.SendHttp(ProtoBuf.Message.OpDefine.CStreasureRebuild, msg, (pack, b) => {
                    if (b)
                    {
                        ProtoBuf.Message.SCtreasureRebuild _msg = pack.kBody as ProtoBuf.Message.SCtreasureRebuild;
                        StaticData.playerData.Delete(_msg.cost);
                        StaticData.playerData.AddResultObj(_msg.get);
                        StaticData.recycleReward = _msg.get;
                        StaticData.ClearRecycleResetSelect();
                        StartCoroutine(ConfirmReset());
                    }
                });
            }

        }


        int curCost;
        void Refresh()
        {
            if (StaticData.IsRecycleResetSelect())
            {
                curCost = 0;
                costObj.SetActive(true);
                switch (StaticData.curRecycleResetSelectType)
                {
                    case 0:
                        {
                            var hero = StaticData.playerData.InstPlayerData.GetUIHero(StaticData.recycleResetSelect[0]);
                            if (hero != null)
                            {
                                ResetIcon.gameObject.SetActive(true);
                                ResetIcon.SetData(hero.heroId, false);

                                var str = Dict.Blo.DictConfigBlo.GetHeroResetCost();
                                var arr = str.Split(',');
                                if (arr.Length == 2)
                                {
                                    costLabel.text = TableTextBlo.GetText("recycle_reset_cost") + arr[1] + TableTextBlo.GetText(CommonMethod.GetObjectNameTextId(arr[0]));
                                    int.TryParse(arr[1], out curCost);
                                }
                                else
                                {
                                    costLabel.text = "";
                                }
                            }
                            else
                            {
                                ResetIcon.gameObject.SetActive(false);
                                costLabel.text = "";
                            }
                            break;
                        }
                    case 1:
                        {
                            var equip = StaticData.playerData.InstPlayerData.GetUIEquipment(StaticData.recycleResetSelect[0]);
                            if (equip != null)
                            {
                                ResetIcon.gameObject.SetActive(true);
                                ResetIcon.SetData(equip.equipId, false, -1, equip);

                                var str = Dict.Blo.DictConfigBlo.GetEquipResetCost();
                                var arr = str.Split(',');
                                if (arr.Length == 2)
                                {
                                    costLabel.text = TableTextBlo.GetText("recycle_reset_cost") + arr[1] + TableTextBlo.GetText(CommonMethod.GetObjectNameTextId(arr[0]));
                                    int.TryParse(arr[1], out curCost);
                                }
                                else
                                {
                                    costLabel.text = "";
                                }
                            }
                            else
                            {
                                ResetIcon.gameObject.SetActive(false);
                                costLabel.text = "";
                            }
                            break;
                        }
                    case 2:
                        {
                            var trea = StaticData.playerData.InstPlayerData.GetUITreasure(StaticData.recycleResetSelect[0]);
                            if (trea != null)
                            {
                                ResetIcon.gameObject.SetActive(true);
                                ResetIcon.SetData(trea.treasureId, false);

                                var str = Dict.Blo.DictConfigBlo.GetEquipResetCost();
                                var arr = str.Split(',');
                                if (arr.Length == 2)
                                {
                                    costLabel.text = TableTextBlo.GetText("recycle_reset_cost") + arr[1] + TableTextBlo.GetText(CommonMethod.GetObjectNameTextId(arr[0]));
                                    int.TryParse(arr[1], out curCost);
                                }
                                else
                                {
                                    costLabel.text = "";
                                }
                            }
                            else
                            {
                                ResetIcon.gameObject.SetActive(false);
                                costLabel.text = "";
                            }
                            break;
                        }
                }
            }
            else
            {
                ResetIcon.gameObject.SetActive(false);
                costObj.SetActive(false);
            }
        }


        bool isShow = false;

        public void CloseTab()
        {
            gameObject.SetActive(false);
        }

        public bool IsShow()
        {
            return isShow;
        }

        public void OnPageClose()
        {
            StaticData.ClearRecycleResetSelect();
        }

        public void OnRemoveCoverPage()
        {
            Refresh();
        }

        public void ReShowTab()
        {
            Refresh();
        }

        public void SetShow(bool isShow)
        {
            this.isShow = isShow;
            gameObject.SetActive(isShow);
        }

        public void ShowTab()
        {
            gameObject.SetActive(true);
            Refresh();
            wheelAnim.Play(RecyclePage.wheelAniNormalName);
        }
    }
}